package hng.controller.store;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import hng.dto.store.*;
import hng.exception.BizException;
import hng.model.ProductSpecDO;
import hng.service.store.StoreService;
import hng.util.JsonData;
import hng.vo.store.PageResult;
import hng.vo.store.ProductExcelVO;
import hng.vo.store.TotalDataVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@RequestMapping("/store")
public class StoreController {

    @Autowired(required = false)
    private StoreService storeService;

    /**
     * 查询商品列表
     */
    @PostMapping("/product/list")
    public JsonData productList(@RequestBody ProductQuery productQuery) {
        PageResult pageResult = storeService.productList(productQuery);
        return JsonData.buildSuccess(pageResult);
    }

    /**
     * 删除商品
     */
    @GetMapping("/product/del-one")
    public JsonData productDelOne(@RequestParam("id") Integer id) {
        storeService.productDelOne(id);
        return JsonData.buildSuccess();
    }

    /**
     * 批量删除
     */
    @PostMapping("/product/del-batch")
    public JsonData productDelBatch(@RequestBody DelBatchDTO delBatchDTO) {
        storeService.productDelBatch(delBatchDTO);
        return JsonData.buildSuccess();
    }

    /**
     * 导出商品的excel表格
     */
    @GetMapping("/export-product")
    public void exportProducts(HttpServletResponse response) throws IOException {
        // 查询商品列表
        List<ProductExcelVO> productList = storeService.getProductList();
        // 设置响应
        response.setContentType("application/vnd.excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("file", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        // 写入数据
        EasyExcel.write(response.getOutputStream(), ProductExcelVO.class).sheet("sheet1").doWrite(productList);
    }

    /**
     * 分页查询商品评论
     */
    @PostMapping("/comment/list")
    public JsonData commentList(@RequestBody CommentQuery commentQuery) {
        PageResult pageResult = storeService.commentList(commentQuery);
        return JsonData.buildSuccess(pageResult);
    }

    /**
     * 分页查询订单
     */
    @PostMapping("/orders/list")
    public JsonData ordersList(@RequestBody OrdersQuery ordersQuery) {
        PageResult pageResult = storeService.ordersList(ordersQuery);
        return JsonData.buildSuccess(pageResult);
    }

    /**
     * 查询首页4个数据
     */
    @GetMapping("/total-data")
    public JsonData totalData() {
        TotalDataVO totalDataVO = storeService.totalData();
        return JsonData.buildSuccess(totalDataVO);
    }

    /**
     * 发货
     */
    @GetMapping("/orders/shipments")
    public JsonData shipments(@RequestParam("orderId") Integer orderId) {
        storeService.shipments(orderId);
        return JsonData.buildSuccess();
    }

    /**
     * 添加商品
     */
    @PostMapping("/product/add")
    public JsonData addProduct(
            @RequestParam("name") String name,
            @RequestParam("price") Double price,
            @RequestParam("detail") String detail,
            @RequestParam("categoryId") Integer categoryId,
            @RequestParam("specListStr") String specListStr,
            @RequestParam("file") MultipartFile[] files) {

        if (files.length == 0) {
            throw new BizException("请上传商品图片");
        }

        ProductDTO productDTO = new ProductDTO();
        productDTO.setName(name);
        productDTO.setPrice(price);
        productDTO.setDetail(detail);
        productDTO.setCategoryId(categoryId);
        // 规格列表
        List<ProductSpecDO> specDOList = JSON.parseArray(specListStr, ProductSpecDO.class);
        if (specDOList.size() == 0) {
            throw new BizException("请添加商品规格");
        }
        productDTO.setSpecList(specDOList);
        storeService.addProduct(productDTO, files);
        return JsonData.buildSuccess();
    }
}
